package com.niguang.labour.common.util;

import com.aliyun.dayu.mode.SendSmsRequest;
import com.aliyun.dayu.mode.SendSmsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

public class SmsUtil {

    private static final String product = "Dysmsapi";
    private static final String domain = "dysmsapi.aliyuncs.com";
    private static final String signName = "汇匠源";
    private static final String accessKeyId = "LTAI0na3Cv18bzKv";
    private static final String accessKeySecret = "BaNcCRYpK6D0GknmIsdrhAoTHn1sVa";

    public static SendSmsResponse sendMessage(String templateCode, String  phoneNumbers, Map<String, Object> params ) {
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            DefaultAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phoneNumbers);
            request.setSignName(signName);
            request.setTemplateCode(templateCode);
            request.setTemplateParam((new ObjectMapper()).writeValueAsString(params));
            request.setOutId("1111");
            return acsClient.getAcsResponse(request);
        } catch (ClientException | JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
